#include "test3.h"

/******************************* order **********************************/
StackStatus StackInit(SqStack *stack)
{
    if( MAXNUM < 1 )
        return N;
    for(int i = 0; i < MAXNUM; i++){
        stack->data[i]=0;
    }
    stack->top = 0;
    return Y;
}
StackStatus InStack(SqStack *stack, StackElemType data)
{
    if(stack->top == MAXNUM)
        return N;
    stack->data[stack->top] = data;
    stack->top++;
}
StackStatus OutStack(SqStack *stack, StackElemType *data)
{
    if( stack->top < 1 )
        return N;
    *data = stack->data[stack->top-1];
    stack->data[stack->top] = 0;
    stack->top--;
    return Y;
}
StackStatus StackFree(SqStack *stack)
{
    free(stack);
    return Y;
}

/******************************* list **********************************/
StackStatus TopStackInit(TopStack *topstack)
{
    topstack->top = NULL;
    topstack->num = 0;
}
StackStatus InListStack(TopStack *topstack, StackElemType data)
{
    StackNode *p = (StackNode *)malloc(sizeof(StackNode));
    p->next = topstack->top;
    p->data = data;
    topstack->top = p;
    topstack->num++;
    return Y;
}
StackStatus OutListStack(TopStack *topstack, StackElemType *data)
{
    if( topstack->num == 0 || topstack->top == NULL){
        printf("ERROR\n");
        return N;
    }       
    StackNode *s;
    s = topstack->top;
    *data = s->data;
    topstack->top = topstack->top->next;
    topstack->num--;
    free(s);
    return Y;
}


void test3(void)
{
    RPN_function();
}



// 后缀表示法 逆波兰
void RPN_function(void)
{
    int i=0;
    int tem1=0, tem2=0, tem3=0;
    char str[32] = "931-3*+42/+";
    int result = 0;
    // int data = 0;
    TopStack stack;
    TopStackInit(&stack);
    
    while( str[i]!='\0' ){
        if( str[i]>='0' && str[i]<='9' ){
            tem3 = str[i]-'0';
            InListStack(&stack, tem3);
            printf("t3=%d\n",tem3);
        }
        else{
            if( str[i]=='+' ){
                OutListStack(&stack, &tem1);
                OutListStack(&stack, &tem2);
                result = (tem2 + tem1);
                InListStack(&stack, result);
                printf("+ t1=%d t2=%d t3=%d return=%d\n",tem1,tem2,tem3,result);
            }
            else if( str[i]=='-' ){
                OutListStack(&stack, &tem1);
                OutListStack(&stack, &tem2);
                result = (tem2 - tem1);
                InListStack(&stack, result);
                printf("- t1=%d t2=%d t3=%d return=%d\n",tem1,tem2,tem3,result);
            }
            else if( str[i]=='*' ){
                OutListStack(&stack, &tem1);
                OutListStack(&stack, &tem2);
                result = (tem2 * tem1);
                InListStack(&stack, result);
                printf("* t1=%d t2=%d t3=%d return=%d\n",tem1,tem2,tem3,result);
            }
            else if( str[i]=='/' ){
                OutListStack(&stack, &tem1);
                OutListStack(&stack, &tem2);
                result = (tem2 / tem1);
                InListStack(&stack, result);
                printf("/ t1=%d t2=%d t3=%d return=%d\n",tem1,tem2,tem3,result);
            }
        }
        i++;
    }
    printf("return = %d\n", result);
}